package org.thirdHero.core.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateHelper {
    public static final String TODAY = "TODAY";
    public static final String YESTERDAY = "YESTERDAY";
    public static final String SEVENDAY = "SEVENDAY";
    public static final String THISMONTH = "THISMONTH";
    public static final String LASTMONTH = "LASTMONTH";

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(DateHelper.getThisMonth().getEndDate()));
        System.out.println(getYestday());
        System.out.println(getSevenday());
        System.out.println(getThisMonth());
        System.out.println(getLastMonth());
        System.out.println(checkIsLastDay(new Date()));
        System.out.println(inFiveMinute(coveStr2Date("2017-10-23 17:50:59")));
    }

    public static boolean inFiveMinute(Date date) {
        Date nowDate = new Date();
        long between = (nowDate.getTime() - date.getTime()) / 1000;//除以1000是为了转换成秒
        long min = between / 60;
        if (min > 5) {
            return false;
        } else {
            return true;
        }
    }

    public static Date coveStr2Date(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(date);
    }

    public static boolean checkIsLastDay(Date nowDate) {
        Date lastDate = DateHelper.lastDayOfMonth(new Date());
        return DateHelper.isToday(nowDate, lastDate);
    }

    public static DateRange getThisMonth() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String firstDay = getThisMonthStartDateStr();
        String lastMonth = getThisMonthLastDateStr();
        String beginDateString = firstDay + " 00:00:00";
        String endDateString = lastMonth + " 23:59:59";
        Date beginDate = sdf.parse(beginDateString);
        Date endDate = sdf.parse(endDateString);
        DateRange dateRange = new DateRange(beginDate, endDate, DateHelper.THISMONTH);
        return dateRange;

    }

    public static DateRange getLastMonth() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastMonth = getLastMonthStartDateStr();
        String lastMonthday = getLastMonthLastDateStr();
        String beginDateString = lastMonth + " 00:00:00";
        String endDateString = lastMonthday + " 23:59:59";
        Date beginDate = sdf.parse(beginDateString);
        Date endDate = sdf.parse(endDateString);
        DateRange dateRange = new DateRange(beginDate, endDate, DateHelper.LASTMONTH);
        return dateRange;

    }

    public static DateRange getYestday() throws ParseException {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        date = calendar.getTime();
        DateRange dateRange = getDateRange(date, DateHelper.YESTERDAY);
        return dateRange;
    }

    public static DateRange getSevenday() throws ParseException {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);
        date = calendar.getTime();
        DateRange dateRange = getDateRange(date, DateHelper.SEVENDAY);
        return dateRange;
    }

    private static DateRange getDateRange(Date date, String dateType) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        if (SEVENDAY.equals(dateType)) {
            dateString = sdf.format(new Date());
        }
        String beginDateString = formatter.format(date) + " 00:00:00";
        String endDateString = dateString + " 23:59:59";
        Date beginDate = sdf.parse(beginDateString);
        Date endDate = sdf.parse(endDateString);
        return new DateRange(beginDate, endDate, dateType);
    }

    public static DateRange getToday() throws ParseException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        String beginDateString = dateString + " 00:00:00";
        String endDateString = dateString + " 23:59:59";
        Date beginDate = sdf.parse(beginDateString);
        Date endDate = sdf.parse(endDateString);
        DateRange dateRange = new DateRange(beginDate, endDate, DateHelper.TODAY);
        return dateRange;
    }

    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null) {
            date = new Date();
        }
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getDateyyyyMMddHHmmss(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        if (date == null) {
            date = new Date();
        }
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getDateNumber() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String dateString = formatter.format(new Date());
        String result = dateString.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
        result = result + StringUtil.getCode();
        return result;
    }

    public static String getThisMonthStartDateStr() {
        Calendar cale = Calendar.getInstance();
        String firstday;
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        firstday = format.format(cale.getTime());
        return firstday;
    }

    public static String getThisMonthLastDateStr() {
        Calendar cale = Calendar.getInstance();
        String lastDate;
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        lastDate = format.format(cale.getTime());
        return lastDate;
    }

    public static String getLastMonthStartDateStr() {
        Calendar cale = Calendar.getInstance();
        String firstday;
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        firstday = format.format(cale.getTime());
        return firstday;
    }

    public static String getLastMonthLastDateStr() {
        Calendar cale = Calendar.getInstance();
        String lastDate;
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        lastDate = format.format(cale.getTime());
        return lastDate;
    }

    public static DateRange getDateRage(String dateState) throws ParseException {
        DateRange dateRange = null;
        if (!dateState.isEmpty()) {
            if (DateHelper.YESTERDAY.equals(dateState)) {
                dateRange = DateHelper.getYestday();
            } else if (DateHelper.THISMONTH.equals(dateState)) {
                dateRange = DateHelper.getThisMonth();
            } else if (DateHelper.LASTMONTH.equals(dateState)) {
                dateRange = DateHelper.getLastMonth();
            } else if (DateHelper.SEVENDAY.equals(dateState)) {
                dateRange = DateHelper.getSevenday();
            } else if (DateHelper.TODAY.equals(dateState)) {
                dateRange = DateHelper.getToday();
            }
        }
        return dateRange;
    }

    /**
     * 判断是否同一天
     *
     * @param sourceDate
     * @param targetDate
     * @return
     */
    public static boolean isToday(Date sourceDate, Date targetDate) {
        if (sourceDate == null) {
            return false;
        }
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(sourceDate);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(targetDate);
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断是否为同一个月
     *
     * @param sourceDate
     * @param targetDate
     * @return
     */
    public static boolean isSameMonth(Date sourceDate, Date targetDate) {
        if (sourceDate == null) {
            return false;
        }
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(sourceDate);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(targetDate);
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH);

    }

    public static void isLastMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayOfMonth = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(lastDayOfMonth));
    }

    public static Date lastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设定当前时间为每月一号
        // 当前日历的天数上-1变成最大值 , 此方法不会改变指定字段之外的字段
        calendar.roll(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 字符串转LocalDate
     *
     * @param dateStr 日期字符串
     * @return
     */
    public static LocalDateTime str2LocalDateTime(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, DateTimeFormatter.ISO_DATE_TIME);
        return localDateTime;
    }

    /**
     * 日期转字符串
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime2Str(LocalDateTime localDateTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
        return df.format(localDateTime);
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static Date longStamp2Date(long s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s + "");
        return date;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * @param strDate 返回java.sql.Time格式的
     */
    public static java.sql.Time strToTimeHHMMSS(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        java.util.Date d = null;
        try {
            d = format.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.sql.Time time = new java.sql.Time(d.getTime());
        return time;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        //设置结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //处于开始时间之后，和结束时间之前的判断
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean compareDate(String beginDate,String endDate) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            //格式化当前时间格式
            nowTime = df.parse(df.format(new Date()));
            //定义开始时间
            beginTime = df.parse(beginDate);
            //定义结束时间
            endTime = df.parse(endDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用判断方法
        return belongCalendar(nowTime, beginTime, endTime);
    }
}
